package com.rainflowerstone.business.components;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmuidemo.base.BaseFragment;
import com.qmuiteam.qmuidemo.lib.Group;
import com.qmuiteam.qmuidemo.lib.annotation.Widget;
import com.qmuiteam.qmuidemo.manager.QDDataManager;
import com.qmuiteam.qmuidemo.model.QDItemDescription;
import com.rainflowerstone.R;
import com.rainflowerstone.adapter.RainFlowerStoneFragmentPagerAdapter;
import com.rainflowerstone.home.components.TestFragment;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ Author     ：lwc
 * @ Date       ：Created in 18:25 2019/1/24
 * @ Description：${description}
 * @ Modified By：
 * @Version: $version$
 */
@Widget(group = Group.Other, name = "分类")
public class AssortmentFragment extends BaseFragment {
    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;
    @BindView(R.id.tabSegment)
    QMUITabSegment mTabSegment;
    @BindView(R.id.contentViewPager)
    ViewPager mContentViewPager;
    private QDItemDescription mQDItemDescription;
    private List<Fragment> mFragmentList;

    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.rain_flower_stone_fragment_assortment, null);
        ButterKnife.bind(this, root);
        mQDItemDescription = QDDataManager.getInstance().getDescription(this.getClass());
        initFragment();
        initTabAndPager();
        initTopBar();
        return root;
    }
    private void initTopBar() {
        mTopBar.addLeftImageButton(R.drawable.anl_selector_common_back_white_btn, R.id.qmui_topbar_item_left_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popBackStack();
            }
        });
        mTopBar.setTitle(mQDItemDescription.getName());

        mTopBar.addRightImageButton(R.mipmap.icon_topbar_overflow, R.id.topbar_right_change_button)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showBottomSheetList();
                    }
                });
    }

    private void showBottomSheetList() {
        new QMUIBottomSheet.BottomListSheetBuilder(getActivity())
                .addItem(getResources().getString(R.string.tabSegment_mode_general))
                .addItem(getResources().getString(R.string.tabSegment_mode_bottom_indicator))
                .addItem(getResources().getString(R.string.tabSegment_mode_top_indicator))
                .addItem(getResources().getString(R.string.tabSegment_mode_indicator_with_content))
                .addItem(getResources().getString(R.string.tabSegment_mode_left_icon_and_auto_tint))
                .addItem(getResources().getString(R.string.tabSegment_mode_sign_count))
                .addItem(getResources().getString(R.string.tabSegment_mode_icon_change))
                .addItem(getResources().getString(R.string.tabSegment_mode_muti_color))
                .addItem(getResources().getString(R.string.tabSegment_mode_change_content_by_index))
                .addItem(getResources().getString(R.string.tabSegment_mode_replace_tab_by_index))
                .setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                    @Override
                    public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                        dialog.dismiss();
//                        switch (position) {
//                            case 0:
//                                mTabSegment.reset();
//                                mTabSegment.setHasIndicator(false);
//                                mTabSegment.addTab(new QMUITabSegment.Tab(getString(R.string.tabSegment_item_1_title)));
//                                mTabSegment.addTab(new QMUITabSegment.Tab(getString(R.string.tabSegment_item_2_title)));
//                                break;
//                            case 1:
//                                mTabSegment.reset();
//                                mTabSegment.setHasIndicator(true);
//                                mTabSegment.setIndicatorPosition(false);
//                                mTabSegment.setIndicatorWidthAdjustContent(true);
//                                mTabSegment.addTab(new QMUITabSegment.Tab(getString(R.string.tabSegment_item_1_title)));
//                                mTabSegment.addTab(new QMUITabSegment.Tab(getString(R.string.tabSegment_item_2_title)));
//                                break;
//                            case 2:
//                                mTabSegment.reset();
//                                mTabSegment.setHasIndicator(true);
//                                mTabSegment.setIndicatorPosition(true);
//                                mTabSegment.setIndicatorWidthAdjustContent(true);
//                                mTabSegment.addTab(new QMUITabSegment.Tab(getString(R.string.tabSegment_item_1_title)));
//                                mTabSegment.addTab(new QMUITabSegment.Tab(getString(R.string.tabSegment_item_2_title)));
//                                break;
//                            case 3:
//                                mTabSegment.reset();
//                                mTabSegment.setHasIndicator(true);
//                                mTabSegment.setIndicatorPosition(false);
//                                mTabSegment.setIndicatorWidthAdjustContent(false);
//                                mTabSegment.addTab(new QMUITabSegment.Tab(getString(R.string.tabSegment_item_1_title)));
//                                mTabSegment.addTab(new QMUITabSegment.Tab(getString(R.string.tabSegment_item_2_title)));
//                                break;
//                            case 4:
//                                mTabSegment.reset();
//                                mTabSegment.setHasIndicator(false);
//                                QMUITabSegment.Tab component = new QMUITabSegment.Tab(
//                                        ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component),
//                                        null,
//                                        "Components", true
//                                );
//                                QMUITabSegment.Tab util = new QMUITabSegment.Tab(
//                                        ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util),
//                                        null,
//                                        "Helper", true
//                                );
//                                mTabSegment.addTab(component);
//                                mTabSegment.addTab(util);
//                                break;
//                            case 5:
//                                QMUITabSegment.Tab tab = mTabSegment.getTab(0);
//                                tab.setSignCountMargin(0, -QMUIDisplayHelper.dp2px(getContext(), 4));
//                                tab.showSignCountView(getContext(), 1);
//                                break;
//                            case 6:
//                                mTabSegment.reset();
//                                mTabSegment.setHasIndicator(false);
//                                QMUITabSegment.Tab component2 = new QMUITabSegment.Tab(
//                                        ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component),
//                                        ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component_selected),
//                                        "Components", false
//                                );
//                                QMUITabSegment.Tab util2 = new QMUITabSegment.Tab(
//                                        ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util),
//                                        ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util_selected),
//                                        "Helper", false
//                                );
//                                mTabSegment.addTab(component2);
//                                mTabSegment.addTab(util2);
//                                break;
//                            case 7:
//                                mTabSegment.reset();
//                                mTabSegment.setHasIndicator(true);
//                                mTabSegment.setIndicatorWidthAdjustContent(true);
//                                mTabSegment.setIndicatorPosition(false);
//                                QMUITabSegment.Tab component3 = new QMUITabSegment.Tab(
//                                        ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component),
//                                        null,
//                                        "Components", true
//                                );
//                                component3.setTextColor(QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_blue),
//                                        QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_red));
//                                QMUITabSegment.Tab util3 = new QMUITabSegment.Tab(
//                                        ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util),
//                                        null,
//                                        "Helper", true
//                                );
//                                util3.setTextColor(QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_gray_1),
//                                        QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_red));
//                                mTabSegment.addTab(component3);
//                                mTabSegment.addTab(util3);
//                                break;
//                            case 8:
//                                mTabSegment.updateTabText(0, "动态更新文案");
//                                break;
//                            case 9:
//                                QMUITabSegment.Tab component4 = new QMUITabSegment.Tab(
//                                        ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component),
//                                        null,
//                                        "动态更新", true
//                                );
//                                mTabSegment.replaceTab(0, component4);
//                                break;
//
//                            default:
//                                break;
//                        }
                        mTabSegment.notifyDataChanged();
                    }
                })
                .build()
                .show();
    }

    /*添加fragment*/
    private void initFragment(){
        mFragmentList=new ArrayList<>();
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
        mFragmentList.add(new TestFragment());
    }

    private void initTabAndPager() {
        mContentViewPager.setAdapter(new RainFlowerStoneFragmentPagerAdapter(getChildFragmentManager(),mFragmentList,12));
        //默认显示第一个TAB
        mContentViewPager.setCurrentItem(0, false);
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 1"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 2"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 3"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 4"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 5"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 6"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 7"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 8"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 9"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 10"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 11"));
        mTabSegment.addTab(new QMUITabSegment.Tab("Item 12"));
        mTabSegment.setupWithViewPager(mContentViewPager, false);
        mTabSegment.setHasIndicator(true);
//        mTabSegment.setMode(QMUITabSegment.MODE_FIXED);
//        以下设置自定义宽度边距
        mTabSegment.setMode(QMUITabSegment.MODE_SCROLLABLE);
        int space = QMUIDisplayHelper.dp2px(getContext(), 25);//item之间的宽度
        int paddingLeftAndRight = QMUIDisplayHelper.dp2px(getContext(), 16);//距离左右边的距离
        mTabSegment.setItemSpaceInScrollMode(space);//设置间距
        mTabSegment.setPadding(paddingLeftAndRight, 0, paddingLeftAndRight, 0);
        mTabSegment.addOnTabSelectedListener(new QMUITabSegment.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int index) {
                mTabSegment.hideSignCountView(index);
            }

            @Override
            public void onTabUnselected(int index) {

            }

            @Override
            public void onTabReselected(int index) {
                mTabSegment.hideSignCountView(index);
            }

            @Override
            public void onDoubleTap(int index) {

            }
        });
    }

    //展示全部TAB项
    @OnClick(R.id.iv_down)
    void down() {
//        final int TAG_SHARE_WECHAT_FRIEND = 0;
//        final int TAG_SHARE_WECHAT_MOMENT = 1;
//        final int TAG_SHARE_WEIBO = 2;
//        final int TAG_SHARE_CHAT = 3;
//        final int TAG_SHARE_LOCAL = 4;
//        RainFlowerStoneTopSheet.TopGridSheetBuilder builder=new RainFlowerStoneTopSheet.TopGridSheetBuilder(getActivity());
//        builder.addItem(R.mipmap.icon_more_operation_share_friend, "分享到微信", TAG_SHARE_WECHAT_FRIEND, RainFlowerStoneTopSheet.TopGridSheetBuilder.FIRST_LINE)
//                .addItem(R.mipmap.icon_more_operation_share_moment, "分享到朋友圈", TAG_SHARE_WECHAT_MOMENT, RainFlowerStoneTopSheet.TopGridSheetBuilder.FIRST_LINE)
//                .addItem(R.mipmap.icon_more_operation_share_weibo, "分享到微博", TAG_SHARE_WEIBO, RainFlowerStoneTopSheet.TopGridSheetBuilder.FIRST_LINE)
//                .addItem(R.mipmap.icon_more_operation_share_chat, "分享到私信", TAG_SHARE_CHAT, RainFlowerStoneTopSheet.TopGridSheetBuilder.FIRST_LINE)
//                .addItem(R.mipmap.icon_more_operation_save, "保存到本地", TAG_SHARE_LOCAL, RainFlowerStoneTopSheet.TopGridSheetBuilder.SECOND_LINE)
//                .setOnSheetItemClickListener(new RainFlowerStoneTopSheet.TopGridSheetBuilder.OnSheetItemClickListener() {
//                    @Override
//                    public void onClick(RainFlowerStoneTopSheet dialog, View itemView) {
//                        dialog.dismiss();
//                        int tag = (int) itemView.getTag();
//                        switch (tag) {
//                            case TAG_SHARE_WECHAT_FRIEND:
//                                Toast.makeText(getActivity(), "分享到微信", Toast.LENGTH_SHORT).show();
//                                break;
//                            case TAG_SHARE_WECHAT_MOMENT:
//                                Toast.makeText(getActivity(), "分享到朋友圈", Toast.LENGTH_SHORT).show();
//                                break;
//                            case TAG_SHARE_WEIBO:
//                                Toast.makeText(getActivity(), "分享到微博", Toast.LENGTH_SHORT).show();
//                                break;
//                            case TAG_SHARE_CHAT:
//                                Toast.makeText(getActivity(), "分享到私信", Toast.LENGTH_SHORT).show();
//                                break;
//                            case TAG_SHARE_LOCAL:
//                                Toast.makeText(getActivity(), "保存到本地", Toast.LENGTH_SHORT).show();
//                                break;
//                        }
//
//                    }
//                }).build().show();



    }
}
